# models.py
from datetime import datetime

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    userAccount = db.Column(db.String(256), unique=True, nullable=False)
    userPassword = db.Column(db.String(512), nullable=False)
    userName = db.Column(db.String(256), nullable=True)
    userProfile = db.Column(db.String(512), nullable=True)
    userRole = db.Column(db.String(256), default='user', nullable=False)
    editTime = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    createTime = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    updateTime = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
    isDelete = db.Column(db.SmallInteger, default=0, nullable=False)

    model_records = db.relationship('ModelRecord', backref='user', lazy=True)

    def __repr__(self):
        return f"User('{self.userAccount}', '{self.userName}')"

class ModelRecord(db.Model):
    __tablename__ = 'modelrecord'
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    userId = db.Column(db.BigInteger, db.ForeignKey('user.id'), nullable=False)
    imageUrl = db.Column(db.String(1024), nullable=False)
    modelUrl = db.Column(db.String(1024), nullable=False)
    createTime = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)

    def __repr__(self):
        return f"ModelRecord('{self.imageUrl}', '{self.modelUrl}')"
